//index.js
//获取应用实例
var app = getApp()
Page({
  data: {
    item: {
      display    :'hide',
      error_info :'授权码不能为空',
    },
    inputAuthCode:'010010',
    userInfo: {}
  },
  //判断本地是否存有授权码，如果存在授权码，绕过授权码验证页面
  
 onShow: function () {
    var that = this;
    //判断缓存中如果存在授权码，则绕过授权码验证页面
    var auth_code = wx.getStorageSync('auth_code') || '';
    var line_state = wx.getStorageSync('line_state'); line_state = line_state == 0 ? 0 : 1;
    if (auth_code != '' && line_state != 0) {
        wx.redirectTo({ url: '/pages/battle/battle' });
    }
  },

  //事件处理函数
  bindViewTap: function() {
    wx.navigateTo({
      url: '../logs/logs'
    })
  },
  onLoad: function () {
    var that = this;
    //调用应用实例的方法获取全局数据
    app.getUserInfo(function(userInfo){
      //更新数据
      that.setData({
        userInfo:userInfo
      })
    })

  },
  //input输入改变
  authCodeChange:function(e){
      this.data.inputAuthCode = e.detail.value;
      console.log(this.data.inputAuthCode);
  },

  //点击登录按钮
  doLogin:function(){
      var that = this;
      var auth_code = this.data.inputAuthCode;
      //验证授权码
      if(this.checkAuthCode(auth_code)){
          wx.showLoading({
              title: '验证中...',
          })
          var url       = getApp().data.servser+'/login/index';

          wx.request({
            url: url,
            data: {
              'auth_code': auth_code,
              'session_id':wx.getStorageSync('session_id')
            },
            header: {
                'content-type': 'application/json'
            },
            method : "GET",
            success: function(res) {
               console.log(res);
               if(res.data.state == 1){
                   //授权码保存在本地缓存中
                   wx.setStorage({
                       key:"auth_code",
                       data:that.data.inputAuthCode
                   });
                   //保存赛事的id
                   wx.setStorage({
                       key:"event_id",
                       data:res.data.event_id
                   });
                   //保存赛事的信息
                   wx.setStorage({
                     key: "event_info",
                     data: res.data.event_info
                   });
                   //保存记录id
                   wx.setStorage({
                       key:"consumption_id",
                       data:res.data.consumption_id
                   });
                   wx.setStorage({
                       key:"line_state",
                       data:1
                   });
                    //微信配置信息
                   wx.setStorage({
                     key: "wxapp_config",
                     data: res.data.wxapp_config
                   });
                   wx.redirectTo({
                       url: '/pages/battle/battle'
                   })
                  

               } else if (res.data.state == 0){
                   that.setData({
                      item: {
                          display    :'show',
                          error_info : res.data.info
                      },
                  })
                  that.closeErrorInfo();
               }else{
                 that.setData({
                   item: {
                     display: 'show',
                     error_info: "服务器忙，请稍后重试"
                    }
                  })
                 that.closeErrorInfo();
               }
            },
            complete:function(){
                wx.hideLoading();
            }

          })
      }
  },

  //验证用户输入的授权码
  checkAuthCode:function(auth_code){
     var that = this;
     if(auth_code != ''){
       console.log(1);
        return true;
        
     }else{
         that.setData({
            item: {
                display    :'show',
                error_info :'授权码不能为空',
             }
         })
         this.closeErrorInfo();
         console.log(2);
         return false;
     }
  },
  
  //定时器关闭提示信息
  closeErrorInfo:function(){
      var that = this;
      setTimeout(function(){
        that.setData({
            item: {display:'hide'}
            })
      },'2000')
  }

})
